package com.hardydou.data.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.github.pagehelper.PageInfo;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.Result;
import com.hardydou.common.result.ResultAssert;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.data.model.YdIcon;
import com.hardydou.data.model.YdTag;
import com.hardydou.data.service.IYdIconServce;
import com.hardydou.framework.log.anno.LogMetaInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.security.PermitAll;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020/7/15
 * @Copyright ：Hardydou.com
 */
@Controller
@LogMetaInfo(module = "YdIcon")
@RequestMapping(YdIconController.REQ_PREFIX)
public class YdIconController extends BaseController {
    final static String REQ_PREFIX = "/data/yd/icon";

    /***
     * 主模块名称
     * @return
     */
    @Override
    public String getModel() {
        return "data";
    }

    /***
     * 子模块名称
     * @see #page(String)
     * @return
     */
    @Override
    public String getSubModel() {
        return "yd/icon";
    }

    @Autowired
    IYdIconServce iconServce;

    @GetMapping("")
    public String index() {
        return page("list");
    }

    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return page("add");
    }


    @ApiIgnore
    @PostMapping("/add")
    @ResponseBody
    public Result add(MultipartFile file, String name, String tagIds) throws IOException {
        String value = IoUtil.read(file.getInputStream(), "utf-8");
        long[] tagIdArr = StrUtil.splitToLong(tagIds, ",");
        List<YdTag> tagList = new ArrayList<>();
        for (int i = 0; i < tagIdArr.length; i++) {
            YdTag tag = new YdTag();
            tag.setTagId(tagIdArr[i]);
            tagList.add(tag);
        }
        YdIcon icon = new YdIcon();
        icon.setName(name);
        icon.setValue(value);
        icon.setTags(tagList);
        iconServce.add(icon);
        return ResultHelper.ok();
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long iconId, Model model) {
        YdIcon icon = iconServce.findById(iconId);
        model.addAttribute("icon", icon);
        return page("edit");
    }

    @ApiIgnore
    @PostMapping("/edit")
    public String edit(YdIcon icon, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        iconServce.update(icon);
        model.addAttribute("icon", icon);
        return page("edit");
    }


    //-----------JSON API---------//
    @ApiOperation(value = "图标列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listData(int pageNum, int pageSize, @RequestParam Map params) {
        List<YdIcon> icons = iconServce.list(params);
        PageInfo<YdIcon> pageInfo = new PageInfo<>(icons);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "删除资源")
    @ApiImplicitParam(name = "iconIds", value = "资源ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result del(String iconIds) {
        long[] iconIdsL = StrUtil.splitToLong(iconIds, ",");
        iconServce.deleteByIconId(iconIdsL);
        return ResultHelper.ok();
    }

}
